package org.example.myleet.rosalind.gc;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Solution {
    public String findHighestGcContent(String input) {
        double highestGcContent = 0.0;
        String resultTag = "";
        String tag = "";
        StringBuilder seq = new StringBuilder();
        String[] pieces = input.split("\n");
        int gc = 0, total = 0;
        for (String piece : pieces) {
            if (piece.startsWith(">")) {
                //这里注意整个输入结尾要为"\n>"使得代码可以完成最后一次计算
                //进行一次GC含量计算
                if (seq.length() > 0) {
                    for (int i = 0; i < seq.length(); ++i) {
                        char c = seq.charAt(i);
                        if (c == '\n') {
                            //换行符不算入总字符中
                            continue;
                        }
                        if (c == 'C' || c == 'G') {
                            ++gc;
                        }
                        ++total;
                    }
                    double gcContent = (double) gc / (double) total * 100.0;
                    if (gcContent > highestGcContent) {
                        highestGcContent = gcContent;
                        resultTag = tag;
                    }
                    seq.delete(0, seq.length());
                }
                tag = piece.substring(1);
                gc = 0;
                total = 0;
                continue;
            }
            //拼接序列
            seq.append(piece);
        }
        BigDecimal gcContent = BigDecimal.valueOf(highestGcContent).setScale(6, RoundingMode.HALF_UP);
        return resultTag + "\n" + gcContent.toPlainString();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
//        System.out.println(solution.findHighestGcContent(">Rosalind_6404\n" +
//                "CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCC\n" +
//                "TCCCACTAATAATTCTGAGG\n" +
//                ">Rosalind_5959\n" +
//                "CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCT\n" +
//                "ATATCCATTTGTCAGCAGACACGC\n" +
//                ">Rosalind_0808\n" +
//                "CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGAC\n" +
//                "TGGGAACCTGCGGGCAGTAGGTGGAAT\n>"));
        System.out.println(solution.findHighestGcContent(">Rosalind_2385\n" +
                "TCAGAAAGGGGGCAAAGCCACAACGGGTTCATTTTGGCCTTATGTAACAACGGGAGCAAA\n" +
                "CGTCTTGACCCCTCCCGACTAAAGATAACTTATTAGGCTAGTTCCGGGGACTGATTACTT\n" +
                "AAAATATGCTCCGTACCGCCCCCAGAGTGATATGGCAACTGTAGTCGAAAGACCTTCGGA\n" +
                "TACCACCTTTTATGTGTACATTCCAACAGAGTACGCTGACTAGCACTGTAGGCAATGAAA\n" +
                "AATTATTTTCCAACAGAAATTATAGTCTCCCGGTAAATATCCCTGCGGACGGTTCGTGTC\n" +
                "TGAGATATGGTAACGGGGAGCTCATTAAAGGTAGAGTAGGAGCCGGACGCTGTCACGGAA\n" +
                "CAACCCACGCCGTCATCGCATCTACTCTGTGGCACCTCCACGTAATACTCGAGGACAAAC\n" +
                "CTTTCGGGTATTGGGCGAATTGTAAATTGTGATACGGACCTATGGCCGAGATGGCGGGTA\n" +
                "GAGGAAGGCCTGTCCAAGTTGATAAACCACCCTTCCCGCCTGGCGTGGGCTCGGGTAGGA\n" +
                "ATGAAGAAAAGCTCTGCGGGACTCCTCGTGGGAACGAGCCCCTTTCTTGTAGAAAACATC\n" +
                "ACCGGACGCAACTTATTACCGCCACAAGCTCATAAGCTTTTCCAAGTGGGATATAAATGC\n" +
                "CACCTATTCAGCGCTTATAATATCTAGTGTGTAACTTGGAACATGAGATAGAACTGATGG\n" +
                "ACCAGGGATCAGGCGTCATATCAAGTCATTATCCCAGTGGCTACTAGGAGCAACCGCACA\n" +
                "GTAGCCGAGGCTCCGGAGCGTCTAACGTTATCGCATATC\n" +
                ">Rosalind_3810\n" +
                "TTCTGCGATTATGCTCTTCAGCTATAGGAACCTTGATGGCGTACAGCGACTCAGTATTAG\n" +
                "AATGTCGCCGTCACCTCGGGGGTTCGTTCTAGTCCATCTAATGAGGCTTTAGGCATCCGG\n" +
                "GAGCCGTTGCTATATAAAATCAGAGAGCCGACTAATGTTGTGGAGAAAACATATTGCTGA\n" +
                "GTATTTGAATGCAGGAGCTGAGTCCATTCCATCTTACGTGAGCAGATTTTACTACTCGGG\n" +
                "CTTACGCTCACTCGTTTATGGAGACAGAGGTTAGAGTCAATGAACCATCATAAAGGGAAG\n" +
                "GTATAGTTGACAACCTAAACAAGAGGGGCAACCGCCCCGCTAGAGGACGGACAACGGAGG\n" +
                "TTCAGCGGATCTGATGAATGTGGGGAGCGGGTAGAGCTTTCAAGCCACTTAAGTTAGGTG\n" +
                "CTCAAATTAAATCGTCTTATGCACGCAGATCTGCCGGTAAGGGTAACAAAGCTGATATCA\n" +
                "GTGTGATTGTGGTTTGCCGCCTCAGAGACAACGGATCATAAGTCCGTTTGCCGGGAACGA\n" +
                "GAGGAGGAATAGGCGGGTAACACGGTTGGTTGTCTTTGGTCCAACTATACCTTGACAAGC\n" +
                "CACCTCGGTCCACTTCGCCTTAGCACCGCGTCGTCCACCCTACGATTAAAAAAGAAGTGG\n" +
                "CAGCGAGGCCGATCGTCATCAGCCAGGATGGACATCCACCTTACTCTCTAACCGCTATGT\n" +
                "GTCCCAATGCCCACTACTCTCTACTGGACACTTTGTTGGCGCTCATGAACTTAGGAACGA\n" +
                "CGGTAATAAGCTCCTGGTTATGAGTAATAGAGAGTCATTCAGAATCGGACATCGAGACCA\n" +
                "AGTCTGGAATTATAGTACAACGATTCTCCGTTCTCACAGTGCAGGACAATCTGGTAGCAA\n" +
                "TATCGGATCCTAAGGGACTTTTTCTCGAACACAATACGGGAGAGTGGGAGTGGACACTAC\n" +
                "CCGCCCCTCAAGCGGTTGAGGCGTGCTAGATCGGAGAG\n" +
                ">Rosalind_8537\n" +
                "GAAGCGTGCCTGCATGGCACGTCAACCCGGCGTGTTTCGGCAAGTCTTCCCCACACACAC\n" +
                "CGCACTTTACGGACGTTCAATCCTAGACGGTGCGACCCTGTGTTAACGATACGGCATCGG\n" +
                "ACCCGGTATTCCGAACACCTGTCCAAGCAGTCGCTGCCCCTGCGGCTCACGTGATTCCAA\n" +
                "GAAGACCCCCTCAGCTTGTTAATAAAGAACGTCCACGTTTGTCTCCTGCATGAAGGCACA\n" +
                "CTATAGCGGCTAATCATACTAGAAACCTACCAGACACACCTGGGGTGTCGGGTTCTAAAG\n" +
                "AGTAATTCCAACCAACAAACAAAAAGAGAGCGCTAAACATATGATTCTATTCTCATACGA\n" +
                "GTAAGTGTTTTCTTGATGACTAGAAGGACCCAACGCCAGTTCCTTGCCAATAGTTCGGGG\n" +
                "TAAAGCCGCCTAGATCTTCCCACCGTGTGAAAGTCCTTAGGAGTTTTTTTAGTAACCCAA\n" +
                "TTCTGCGGCCTACCATCCATGTTAATAGAAAATGCGCAAACATATGGTATCAGATAACAA\n" +
                "CTACAAACGTAGATGGGGGGCATCCCAGGAGATAAAACGGGTTGAGAGAGTCATGACTCG\n" +
                "CATACGCTCTAAGGATCCAACCATGGACTTTTAACGCGTACCGATACCAATCGAGCTCGA\n" +
                "GGACCAAGTCAGGCAGCTATATGGAACTACAGGGTGCTTGACCATCACTTGCTCCCACGG\n" +
                "GCGGATCTCATTACGCGAGCGAGAGTATAGTAGAGGAAAAATGTGGGTATTGCAGCTCTA\n" +
                "TGACTCGTAGCGGGCGGCAGCCCAGAAGTCTTCTCCCGA\n" +
                ">Rosalind_1590\n" +
                "CCCCGGACTAAATGTAGGTGACCGTCATTGAGCACTCCGCATCCGTCCCACGCCGTCCAG\n" +
                "CTCACCGCCGTGCACCACGTCCACGCCGAAGATAATGAGTATCGTTGCTGCCCCCGTCGG\n" +
                "CATGAGATAAGGATCCTTCGACGCGGTCGACCGTGGGGTCGATAGCCCGTAGAAGACATG\n" +
                "CGAATAAGATGCAGACTTCTGACTTAACACATTCTTCGCGGAAATCGTACTACTTCTCCT\n" +
                "GAATAGCCGCATTTGAATTGCCACAATGACATATGTGAACGAGTTTGAGAAGCTCTATTA\n" +
                "CGTCGTTCCATTTCCCTCTGCAGGCCTTGTCGCTTATACCCTCCCTGCTACATTTGGCCA\n" +
                "TGAGCGCGTTAGCGATATTACCACTGCTCTTACTTTTCGATGATGCTTCCCGTTGCCAGT\n" +
                "GCAGGAAGGCAGTGGGTTGAGACAACGGAGCATCGAGAGCGTTGAGCATCTATGGACACT\n" +
                "TGCCCATCGGACCAAAGACACCTCTTCATTGGGTGTGTACTTCGCATACCCACTGCCAAG\n" +
                "AACCCTTAGGCACTGTTTTGACCTGCTGGTATCAGAAAAGAGACCCGGAACGGATCTGTT\n" +
                "CCCCGACCCGCGCCCATAACGTACTGGAATCGTAAACCGCATGTCAAGGCTAATGCATCA\n" +
                "TGGCCGTTGTGACCCAGGTCTAATGATACGTCAGTACCTCGATAGCGTGAGCCGAGATAT\n" +
                "CAAGAGGCAGCCTTCCACTGAGCACCTCTCAAACAGAACCGCGCATGTTGTCGGACACCA\n" +
                "TCCAGGTCGTGTAACACCAATAAACCGATGCCGGCTGGGCGCAGCTTTGCTCGATCTAGC\n" +
                "AGCTTTTACTGATTAGATTCATGGTAA\n" +
                ">Rosalind_7899\n" +
                "GGTGCCCTCCTTCAGCGATGACTAATTACTATAATTATCCGCTCAATTCATTGGTGCAAA\n" +
                "GGCGTAACTGGGTGGCCGATGCGGGCGAGCCCGTTTGTTGGGAAACCCCCAGGCGATATG\n" +
                "TGGCAGAACAGCTTTTTCATGCAGCAACGCCATGATGATACTGACCACTTCAATTAGCTC\n" +
                "GATGCTGAATAAGTTATCCCCCATGAGTCGTTAAGTAATCATAGTTTGGGGGCTCCCATT\n" +
                "ACTTACACCGATCCCCTGTTTGTGCAAGACGACCAAGTCGAGGCCGCTGATCTTGGCCGT\n" +
                "ACAATAAGACGCCAGCCGTCTGGAGCCTCTCGTACAACGCAGAAGGTCTTATAGCCAATG\n" +
                "GTGCGGACTTGCTGGTTCGCCATAAAGCTAGCGGTGTGGTTCGGGTCAGCTCCGGTCTGA\n" +
                "CGCAAGAGATGGGGCGGATAAACAAGCGGGCTACTACCGCCACCATCCGGGCTGGAATTG\n" +
                "TCACAAATCGCGGGGGAGTGACACAGGTTCGCCCCTAATTGTGCAAAAGTTTATACTATA\n" +
                "TGGGCAGGCAACACCCTTGTTACTTGCGCGCTGTGTGTCAATTCCAATTCACGCGGTATT\n" +
                "TACGTGCAGCGCAAGACCTCCTATCCCTTCCTAGGAATGGAAATAATGTCAGGTTTGTTG\n" +
                "GGCTTTCAGGGTGGTAGCGTCATGGTTCCATAGTTTTCACCTTTGGTATTGCACCTTAGC\n" +
                "TTATACTCCCATCATAGGACTACTTGATAGCCGAACAAGTGGTCCCCTCGTGACCGCTGG\n" +
                "ACCCGCCAACGTGCCACGGCGAGATCTATTCGAACATTTGGACAGACCTGCGAGCCCTCA\n" +
                "ATGCCGCATTTCTACCACTAACTTGAGGTGAAAGGAAGGTGCTCGTCACTAGCCACGCCT\n" +
                "ACACGGTCAACGGTGATTCTAAGGCCAGCTCAGACGGACACCTGTCGTGTTTTTGTGACT\n" +
                "TTTAAGACTCCCTGTTACTTTCTCCCCCTGGAGCGT\n>"));
    }
}
